package com.jt.tenant.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.FreewayOrbitActivity;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetFreeWayDetail;
import com.jt.tenant.widget.Tools;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/7.
 */
public class FreewayDetailAdapter extends BaseAdapter {

    private Context context;

    private List<GetFreeWayDetail.DetailItem> list = new ArrayList<>();

    private String date;

    public FreewayDetailAdapter(Context context, List<GetFreeWayDetail.DetailItem> list) {

        this.context = context;

        this.list = list;

    }

    @Override
    public int getCount() {

        return list.size();

    }

    @Override
    public Object getItem(int position) {

        return null;

    }

    @Override
    public long getItemId(int position) {

        return position;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_freeway_order_detail, null);

            JTApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);

            holder = new ViewHolder();

            holder.freeway_num = (TextView) convertView.findViewById(R.id.freeway_num);
            holder.freeway_start = (TextView) convertView.findViewById(R.id.freeway_start);
            holder.freeway_end = (TextView) convertView.findViewById(R.id.freeway_end);
            holder.freeway_time = (TextView)convertView.findViewById(R.id.freeway_time);
            holder.freeway_money = (TextView) convertView.findViewById(R.id.freeway_money);
            holder.freeway_btn = (TextView) convertView.findViewById(R.id.freeway_btn);

            holder.freeway_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    context.startActivity(new Intent(context, FreewayOrbitActivity.class).putExtra("item", (Serializable) list.get(position)));

                }
            });

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        holder.freeway_num.setText(list.get(position).id);

        holder.freeway_start.setText(list.get(position).start_point);

        holder.freeway_end.setText(list.get(position).end_point);

        long time = (Long.parseLong(list.get(position).end_time) - Long.parseLong(list.get(position).start_time))/1000/60;

        if(time/60/24 >0){

            date = time/60/20+"天"+time/60+"小时"+time%60+"分";

       }else if(time/60>0){

            date = time/60+"小时"+time%60+"分";

        }else {

            date = String.valueOf(time)+"分";

        }

        holder.freeway_time.setText(date);

        holder.freeway_money.setText(Tools.F_num(list.get(position).money)+"元");

//        holder.freeway_money.setText("11元");

        return convertView;

    }

    class ViewHolder {

        private TextView freeway_num,freeway_start, freeway_end, freeway_time, freeway_money;

        private TextView freeway_btn;

    }
}